<?php

namespace Biz\model;

/**
 *
 * @author lee
 *
 */

use Biz\model\BankList as mBankList;
use Biz\validate\ShopBankInfo as vShopBankInfo;


class ShopBankInfo extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_bank_info';

    const _SUB_TABLE = false;


    public static function createOrEdit($uid, $data, $scenes = null, $method = null)
    {
        try {

            if (is_null($method)) {

                $has = self::has_master(['uid' => $uid]);

                $method = $has ? 'update' : 'insert';
            }

            if (!is_null($scenes)) {
                //提交字段进行校验
                if (!vShopBankInfo::validate($data, $scenes)) {

                    $msg = vShopBankInfo::firstError();

                    throw new \Exception($msg);
                }
            }


            switch ($method) {
                case 'insert':
                    $data['shop_id'] = $uid;
                    $data['uid'] = $uid;
                    $data['addtime'] = date('Y-m-d H:i:s');

                    $ret = self::insert($data);

                    break;
                case 'update':
                    $ret = self::update($data, ['uid' => $uid]);

                    break;
            }

            if ($ret->errorCode() !== '00000') {
                throw new \Exception('数据库操作错误');
            }

            return [$method => $ret->rowCount()];

        } catch (\Exception $e) {


            throw $e;
        }
    }

    //查询银行信息
    public static function getInfo($uid)
    {
        $info = self::get("*", ['uid' => $uid]);

        self::formatInfo($info);

        return $info;
    }

    public static function formatInfo(&$info)
    {
        if ($info) {
            foreach ($info as $key => $value) {
                switch ($key) {
                    case 'bank_name':
                        $localBankList = mBankList::getLocalBankList();
                        $info['bank_name'] = $localBankList[$info['bank_name']]['name'];
                }
            }
        }
    }

    public static function getInfoList($uids)
    {

        $list = self::select("*", ['uid' => $uids]);
        $formatData = [];
        foreach ($list as $info) {
            self::formatInfo($info);

            $formatData[$info["uid"]] = $info;
        }
        return $formatData;
    }


}

